//
//  CenterController.m
//  SizableExample
//
//  Created by Tom Adriaenssen on 14/01/12.
//  Copyright (c) 2012 Adriaenssen BVBA. All rights reserved.
//

#import "CenterController.h"

@implementation CenterController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //Chargeons la vue par défaut au lancement de l'app
    firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 25, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.view insertSubview:firstView belowSubview:toolbar];
    pageTree = [[NSMutableArray alloc] initWithObjects:firstView, nil];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (IBAction)center:(id)sender
{
    [self recieveCenter];
}

-(IBAction)back:(id)sender
{
    [self removeLastPage];    
}

-(IBAction)hrefButton:(id)sender{
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate href:@"ça marche!"];
}

- (void)recieveCenter{
    NSLog(@"center");
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}





/* ----------------------------------------- */
/* ------- FONCTIONS DE NAVIGATION --------- */
/* ----------------------------------------- */

/* --------- Affiche une page en fonction du lien d'appel ------*/


-(void)addView:(NSString *)page{
    
   
    if ([page isEqualToString:@"serie"]){
        pageController = [[PageController alloc] init];
        theView=pageController.view;
        NSLog(@"Ajout d'une page serie");
    }
    [self showView:theView];
    
}


- (void)showView:(UIView *)newPageView{
    
    // Vue actuelle
    UIView *currentView = [pageTree lastObject];
    
    
    // Fenetre qui va bouger
    UIView *theWindow = currentView;
    
    
    //Nouvelle View
    UIView *newView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    //Ajoute du chargement à la nouvelle vue
    //[newView addSubview:chargementView];
    //Ajoute la webView derrière le chargement
    
    [newView insertSubview:newPageView belowSubview:chargementView];
    
    
    //Enregistrement de la vue dans le tableau de navigation des vues
    [pageTree addObject:newView];
    
    //Si on a commencé a naviguer alors on ajoute le bouton retour
    if([pageTree count]>1)  {[self.view insertSubview:back aboveSubview:series];}
    
    // Ajoute la nouvelle vue
    [theWindow addSubview:newView];
    
    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.3];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    
    [[theWindow layer] addAnimation:animation forKey:@"FronSwitch"];
}




/* --------- BACK retourne en arrière et supprime la view ------*/

-(void)removeLastPage{
    
    // Vue actuelle
    UIView *currentView = [pageTree lastObject];
    
    //On supprime la view dans l'arborescence des views
    [pageTree removeLastObject];
    
    // Fenetre qui va être affichée
    UIView *theWindow = [pageTree lastObject];
    
    
    [currentView removeFromSuperview];
    
    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.3];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromLeft];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [[theWindow layer] addAnimation:animation forKey:@"BackSwitch"];
    //Si on a supprimé toutes les pages on retire le logo back
    if([pageTree count]==1) [self.view insertSubview:back belowSubview:series];
    
}

/* --------- Supprime toutes les vues actives en lançant xfois la commande back ------*/
-(void)removeAllPages{
    while ([pageTree count]!=1){
        [self removeLastPage];
    }
}

/* -------------------------------- Supprime le chargement ---------------------------*/
-(void)endLoading{
    
    [UIView animateWithDuration:0.3
                     animations:^{chargementView.alpha = 0.0;}
                     completion:^(BOOL finished){ [[pageTree lastObject] sendSubviewToBack:chargementView]; chargementView.alpha = 1.0;}];
    
}




@end
